//
//  RegisteredViewController.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/9.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "RegisteredViewController.h"
#import "RegisterPassWordViewController.h"
#import "AgreementViewController.h"
//#import ""
@interface RegisteredViewController ()<UITextFieldDelegate,UIAlertViewDelegate>
@property (nonatomic,strong) UITextField *telTextField;
@end

@implementation RegisteredViewController
@synthesize telTextField;
- (void)viewDidLoad {
    [super viewDidLoad];
    TABHIDDEN
//    [self.navigationController setNavigationBarHidden:NO];
    [self.view setBackgroundColor:UIColorFromRGB(0xf2f2f2)];
    self.title = @"注册";
    [self setView];
    [ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
    // Do any additional setup after loading the view.
}
-(void)textChange
{
    UIButton *loginBtn = (UIButton *)[self.view viewWithTag:12];
    UIButton *selectBtn = (UIButton *)[self.view viewWithTag:13];
    if (self.telTextField.text.length ==11 && selectBtn.selected == YES) {
        if (![Unity isValidateTelphone:self.telTextField.text]) {
            [loginBtn setEnabled:NO];
            return;
        }
        [loginBtn setEnabled:YES];
    }else{
        [loginBtn setEnabled:NO];
    }
}
-(void)setView{
    UIView *navView = [self createViewFrame:CGRectMake(0, 0, SWIDTH, 64) bgColor:UIColorFromRGB(0x247ac4) tag:0];
    [self.view addSubview:navView];
    [self.leftBtn setHidden:NO];
    UIButton *btn = [UIButton buttonWithType:0];
    btn.frame = CGRectMake(20, 36.5, 7, 14);
    [btn setBackgroundImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [btn addTarget: self action: @selector(backClick:) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [self.titleLab setText:@"注册"];
    [self.titleLab setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:self.titleLab];
    
    UIButton *btn1 = [UIButton buttonWithType:0];
    btn1.frame = CGRectMake(20, 20, 40, 44);
    [btn1 setBackgroundColor:[UIColor clearColor]];
    [btn1 addTarget:self action: @selector(backClick:) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    UIView *tempView = [self createViewFrame:CGRectMake(10, 77, SWIDTH - 20, 43) bgColor:UIColorFromRGB(0xffffff) tag:0];
    tempView.layer.masksToBounds = YES;
    tempView.layer.cornerRadius = 6.0;
    tempView.layer.borderWidth = 0.6;
    tempView.layer.borderColor = [[UIColor whiteColor] CGColor];
    [self.view addSubview:tempView];
    
    UIImageView *img1 = [self createImageViewFrame:CGRectMake(15, 11, 13, 20) image:nil imageName:@"登录-手机icon" tag:0];
    [tempView addSubview:img1];
    
    self.telTextField = [self createTextFieldFrame:CGRectMake(36, 0, SWIDTH - 53, 42) bgColor:[UIColor clearColor] tag:10 keyBoardType:0];
    self.telTextField.textAlignment = NSTextAlignmentLeft;
    self.telTextField.delegate = self;
    self.telTextField.placeholder = @"手机号";
    self.telTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.telTextField.clearButtonMode = UITextFieldViewModeAlways;
    self.telTextField.font = FONT14;
    [tempView addSubview:self.telTextField];
    
    UIButton *loginBtn = [self createButtonFrame:CGRectMake(10, 140, SWIDTH - 20, 43) title:@"获取验证码" viewController:self selector:@selector(loginClick:) tag:12];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"登录-点击后按钮"] forState:UIControlStateNormal];
    [loginBtn setEnabled:NO];
    loginBtn.titleLabel.font = FONT14;
    [self.view addSubview:loginBtn];
    
    UIButton *selectBtn = [self createButtonFrame:CGRectMake(10, 198, 13, 13) title:nil viewController:self selector:@selector(selectClick:) tag:13];
    selectBtn.selected = YES;
    [selectBtn setBackgroundImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    [self.view addSubview:selectBtn];
    
    UILabel *lab1 = [self createLabelFrame:CGRectMake(27, 198, 55, 13) title:@"我同意" titleColor:UIColorFromRGB(0xbcbcbc) tag:0];
    [lab1 setFont:[UIFont systemFontOfSize:14]];
    [self.view addSubview:lab1];
    
    UIButton *lab2 = [self createButtonFrame:CGRectMake(55, 198, 180, 13) title:@"《有材网APP用户协议》" viewController:self selector:@selector(forgetClick:) tag:14];
    [lab2 setTitleColor:UIColorFromRGB(0x4f97e6) forState:UIControlStateNormal];
//    lab2.titleLabel.textAlignment = NSTextAlignmentLeft;
    [lab2.titleLabel setTextAlignment:NSTextAlignmentLeft];
    [lab2.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.view addSubview:lab2];
    [self.telTextField becomeFirstResponder];
}
//-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    UIButton *loginBtn = (UIButton *)[self.view viewWithTag:12];
//    if (textField == self.telTextField) {
//        NSString *checkString;
//        if (range.location == 11) {
//            [loginBtn setEnabled:YES];
//            return NO;
//        }else{
//            if (![string isEqualToString:@""]) {
//                if (range.location == 10) {
//                    [loginBtn setEnabled:YES];
//                }else{
//                    [loginBtn setEnabled:NO];
//                }
//                checkString=[self.telTextField.text stringByAppendingString:string];
//            }else{
//                checkString=[checkString stringByDeletingLastPathComponent];
//                [loginBtn setEnabled:NO];
//            }
//            return YES;
//        }
//    }
//    return YES;
//}
-(void)resignFirstText:(UITapGestureRecognizer *)tap{
    RESIGNFIRSTRESPONDER(self.telTextField);
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    RESIGNFIRSTRESPONDER(self.telTextField);
}

-(void)backClick:(UIButton *)sender{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册尚未完成，是否继续操作" delegate:self cancelButtonTitle:@"继续" otherButtonTitles:@"退出", nil];
    [alert setTag:110];
    [alert show];
    
}
-(void)forgetClick:(UIButton *)sender{
    // TODO:协议显示
    AgreementViewController *agreementVC =[[AgreementViewController alloc]init];
    agreementVC.title = @"有材协议";
    [self.navigationController pushViewController:agreementVC animated:YES];
}
-(void)selectClick:(UIButton *)sender{
    //TODO: 选择按钮
    UIButton *button =(UIButton *)sender;
    UIButton *loginBtn = (UIButton *)[self.view viewWithTag:12];
    if (button.selected == NO) {
        button.selected = YES;
        [button setBackgroundImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
        if (self.telTextField.text.length != 0 && button.selected == YES) {
            [loginBtn setEnabled:YES];
        }else{
            [loginBtn setEnabled:NO];
        }
        
    }else{
        button.selected = NO;
        [button setBackgroundImage:[UIImage imageNamed:@"未选择"] forState:UIControlStateNormal];
        if (self.telTextField.text.length != 0 && button.selected == YES) {
            [loginBtn setEnabled:YES];
        }else{
            [loginBtn setEnabled:NO];
        }
    }
}
-(void)loginClick:(UIButton *)sender{
    //TODO: 获取验证码
    UIButton *selectBtn = (UIButton *)[self.view viewWithTag:13];
    if (!selectBtn.selected) {
        SHOW_ALERT(@"请选择同意协议");
        return;
    }
    if ([self.telTextField.text isEqualToString:@""] || self.telTextField.text.length != 11) {
        SHOW_ALERT(@"请输入手机号");
        return;
    }
    if (![Unity isValidateTelphone:self.telTextField.text]) {
        SHOW_TITLEALERT(@"提示", @"请输入正确手机号");
        return;
    }
    [self getSms_code];
}
-(void)getSms_code{
    TIME
    HEADDIC(nowTime, SMS_CODE);
    NSDictionary *body = @{
                           @"mode":@"1",
                           @"mobile":self.telTextField.text,
                           };
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:HEAD forKey:@"head"];
    [dic setObject:body forKey:@"body"];
    requestManage *request = [[requestManage alloc]init];
    NSString *result = [request post:APPIP postDataDic:dic error:nil];
    NSLog(@"result___%@",result);
    baseAnalysis *model = [[baseAnalysis alloc]init];
    [model parseResponseData:result];
    if ([model.err_code integerValue]!=0) {
        return;
    }
    RegisterPassWordViewController *regisPwdVC = [[RegisterPassWordViewController alloc]init];
    regisPwdVC.tel = self.telTextField.text;
    regisPwdVC.title = @"注册";
    [self.navigationController pushViewController:regisPwdVC animated:YES];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 110) {
        if (buttonIndex == 1) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }
}
-(void)alertClick:(UIButton *)sender{
//    ALT_MSG(@"弹出");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
